package com.itheima02;

public class Demo2 implements Runnable {
    private int tickets = 100; //共享数据

    @Override
    public void run() { //窗口1 窗口2
        while (true) {
            try {
                Thread.sleep(200);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //同步代码块 窗口1上了一把锁。即使窗口2抢到了执行权力，但是必须等窗口1执行完毕窗口2才能继续执行
            synchronized (this) {
                if (tickets <= 0) {
                    break;
                }
            }
            String name = Thread.currentThread().getName();
            System.out.println(name + "正在卖第" + (100 - tickets + 1) + "张票 ,还剩" + --tickets + "张票");
        }
    }
}
